#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    map=new Astar("Map",this,10,10);
    /*
    QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect;
     //阴影色，透明度
    shadowEffect->setColor(QColor(100,100,100));
    shadowEffect->setBlurRadius(20);//阴影模糊半径
    shadowEffect->setOffset(20);//阴影的偏移值
    map->setGraphicsEffect(shadowEffect);
    */
    mainarea=new QScrollArea(this);
    mainarea->setWidget(map);
    //this->setMinimumSize(mainarea->width(),this->height());

    setMenuBar();
    setToolBar();
    setCentralWidget(mainarea);

}

MainWindow::~MainWindow()
{

}
void MainWindow::setMenuBar(){
    QMenu *fileMenu=menuBar()->addMenu("文件");
    QMenu *settingMenu=menuBar()->addMenu("Settings");
    QMenu *aboutMenu=menuBar()->addMenu("Other");
    QAction *setxyAction=new QAction("设置地图大小");
    QAction *fileopenAction=new QAction("Open File");
    QAction *filesaveAction=new QAction("Save As...");
    QAction *aboutAction=new QAction("About");

    fileMenu->addAction(fileopenAction);
    fileMenu->addAction(filesaveAction);
    fileMenu->addSeparator();


    aboutMenu->addAction(aboutAction);
    aboutMenu->addSeparator();

    settingMenu->addAction(setxyAction);
    settingMenu->addSeparator();

    connect(setxyAction,SIGNAL(triggered(bool)),this,SLOT(openxyDialog()));

}
void MainWindow::openxyDialog(){
    SetxyDialog *aa=new SetxyDialog(this);
    //qDebug()<<map;
    if(aa->exec()==QDialog::Accepted){
        delete map;
        map=new Astar("Map",this,aa->x,aa->y);
        setpainter();
        mainarea->setWidget(map);
        //map->show();
        //qDebug()<<map;
    }
    delete aa;
    return;
}
void MainWindow::clearmap(){
    delete map;
    map=new Astar("Map",this,map->w,map->h);
    //map->show();
    setpainter();
    mainarea->setWidget(map);
    //qDebug()<<"clear";
}
void MainWindow::setToolBar(){
    QToolBar *barset=addToolBar("设定");
    QAction *clearAction=new QAction("清空");
    barset->addAction(clearAction);
    connect(clearAction,SIGNAL(triggered(bool)),this,SLOT(clearmap()));
    startbtn=new QRadioButton(this);
    startbtn->setText("设置起点");
    endbtn=new QRadioButton(this);
    endbtn->setText("设置终点");
    blocksbtn=new QRadioButton(this);
    blocksbtn->setText("设置障碍");
    barset->addWidget(startbtn);
    barset->addWidget(endbtn);
    barset->addWidget(blocksbtn);
    connect(startbtn,SIGNAL(clicked(bool)),this,SLOT(setpainter()));
    connect(endbtn,SIGNAL(clicked(bool)),this,SLOT(setpainter()));
    connect(blocksbtn,SIGNAL(clicked(bool)),this,SLOT(setpainter()));

    startbtn->click();

    QAction *startAstar=new QAction("run");
    barset->addAction(startAstar);
    connect(startAstar,SIGNAL(triggered(bool)),this,SLOT(startA()));
    QAction *clearways=new QAction("清除路径");
    barset->addAction(clearways);
    connect(clearways,SIGNAL(triggered(bool)),this,SLOT(clearways()));

    /*
    QToolButton *test=new QToolButton(this);
    test->setText("test");
    QToolButton *test2=new QToolButton(this);
    test2->setText("test2");
    test->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    QButtonGroup* btngroup=new QButtonGroup(this);
    btngroup->addButton(test);
    btngroup->addButton(test2);
    test->setCheckable(true);
    test2->setCheckable(true);


    barset->addWidget(test);
    barset->addWidget(test2);
    */

    barset->setStyleSheet("QToolBar{background-color:rgb(230,230,230);spacing:10;}");

    barset->setMinimumHeight(30);


}
void MainWindow::setpainter(){
    if(startbtn->isChecked()) map->setpainterstatus(2);
    if(endbtn->isChecked()) map->setpainterstatus(3);
    if(blocksbtn->isChecked()) map->setpainterstatus(0);
}
void MainWindow::startA(){
    map->runAstar();
}
void MainWindow::clearways(){
    map->clearways();

}

